package com.hualude.roadsign.service.impl;

import com.hualude.roadsign.dto.DTMonitorFaultDto;
import com.hualude.roadsign.dto.DTRequestDto;
import com.hualude.roadsign.dto.DTResponseDto;
import com.hualude.roadsign.dto.MonitorFaultDto;
import com.hualude.roadsign.repository.MonitorFaultRepository;
import com.hualude.roadsign.repository.UserRepository;
import com.hualude.roadsign.service.MonitorFaultHandleService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by ding on 2017/8/7.
 */
@Service
public class MonitorFaultHandleServiceImpl implements MonitorFaultHandleService {

    @Resource
    private MonitorFaultRepository monitorFaultRepository;
    @Resource
    private UserRepository userRepository;

    public DTResponseDto getMonitorFaultHandle4DT(DTRequestDto dtRequestDto, String actorIds, Integer orgId, Integer monitorMaintenanceStatus) throws Exception {
        List<DTMonitorFaultDto> dto4DTs = monitorFaultRepository.getMonitorFaultHandle4DT(dtRequestDto,actorIds, orgId,monitorMaintenanceStatus);
        Integer count = monitorFaultRepository.getMonitorFaultHandleCount4DT(dtRequestDto,actorIds, orgId,monitorMaintenanceStatus);
        DTResponseDto dtResponseDto = new DTResponseDto(dtRequestDto.getDraw(), count, count, dto4DTs);
        for(DTMonitorFaultDto dtMachineFaultDto:dto4DTs){
            if(dtMachineFaultDto.getMonitorMaintainerId()!=null){
                String maintainerName =userRepository.getUserNameByUserId(dtMachineFaultDto.getMonitorMaintainerId());
                dtMachineFaultDto.setMonitorMaintainerName(maintainerName);
            }
        }
        return dtResponseDto;
    }

    @Transactional
    public Integer addMonitorMaintenanceContent(Integer lampFaultId, String lampMaintenanceContent) throws Exception {
        return monitorFaultRepository.addMonitorMaintenanceContent(lampFaultId,lampMaintenanceContent);
    }

    @Transactional
    public Integer finishMonitorMaintenance(String monitorMaintenancePhoto,Integer monitorMaintainerId,String monitorMaintenanceTime,String monitorMaintenanceExplain,Integer monitorFaultId) throws Exception {
        return monitorFaultRepository.finishMonitorMaintenance(monitorMaintenancePhoto,monitorMaintainerId,monitorMaintenanceTime,monitorMaintenanceExplain,monitorFaultId);
    }

    public List<MonitorFaultDto> getMonitorFaultHandleList4App(Integer orgId, Integer maintenanceStatus, String actorIds, Integer count, Integer size) throws Exception {
        return monitorFaultRepository.getMonitorFaultHandleList4App(orgId, maintenanceStatus, actorIds, count, size);
    }

    public MonitorFaultDto getMonitorReceiptInfo4App(Integer monitorFaultId) throws Exception{
        return monitorFaultRepository.getMonitorReceiptInfo4App(monitorFaultId);
    }
}
